/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of MansionOfMadnessToolKit.
 *
 * MansionOfMadnessToolKit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MansionOfMadnessToolKit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MansionOfMadnessToolKit.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.diceroller.activitytab.game;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.amphiprion.diceroller.ApplicationConstants;
import org.amphiprion.diceroller.LauncherActivity;
import org.amphiprion.diceroller.R;
import org.amphiprion.diceroller.activitytab.ILoadTask;
import org.amphiprion.diceroller.activitytab.IMenuProvider;
import org.amphiprion.diceroller.activitytab.LoadDataListener;
import org.amphiprion.diceroller.activitytab.PaginedListActivity;
import org.amphiprion.diceroller.activitytab.PaginedListContext;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

public class GameListActivity extends PaginedListActivity<GameLocal> implements IMenuProvider {
	private LinearLayout ln;

	/**
	 * 
	 */
	public GameListActivity() {
		super(new PaginedListContext<GameLocal>(1000), R.layout.item_list, R.id.scroll_view, R.id.item_list, R.string.games_title);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ln = (LinearLayout) findViewById(listViewId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.amphiprion.mansionofmadness.activity.PaginedListActivity#loadDatas (int, int)
	 */
	@Override
	protected List<GameLocal> loadDatas(int page, int pageSize) {
		return getGames(this);
	}

	public static List<GameLocal> getGames(Context context) {
		List<GameLocal> games = new ArrayList<GameLocal>();
		String[] dirs = new File(ApplicationConstants.ROOT_DIRECTORY + "/games").list();
		for (int i = 0; i < dirs.length; i++) {
			File f = new File(ApplicationConstants.ROOT_DIRECTORY + "/games/" + dirs[i] + "/game-definition.xml");
			if (f.exists()) {
				GameLocal gl = new GameLocal(dirs[i], ApplicationConstants.ROOT_DIRECTORY + "/games/" + dirs[i]);
				games.add(gl);
			}
		}
		return games;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.amphiprion.mansionofmadness.activity.PaginedListActivity#getTask( org.amphiprion.mansionofmadness.activity.LoadDataListener, int, int)
	 */
	@Override
	protected ILoadTask<GameLocal> getTask(LoadDataListener<GameLocal> l, int page, int pageSize) {
		return new LoadGameLocalTask(l, page, pageSize);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.amphiprion.mansionofmadness.activity.PaginedListActivity# getDataSummaryView(java.lang.Object)
	 */
	@Override
	protected View getDataSummaryView(GameLocal data) {
		final GameLocalSummaryView view = new GameLocalSummaryView(this, data);
		view.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(GameListActivity.this, LauncherActivity.class);
				intent.putExtra("GAME_DIR", view.getGameLocal().getPath());
				startActivity(intent);

				// int i = ln.indexOfChild(view);
				// if (!view.isExpanded()) {
				// TextView tv = new TextView(GameListActivity.this);
				// LayoutParams tlp = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
				// tv.setLayoutParams(tlp);
				// tv.setText(view.getGameLocal().getHelpDetailId());
				// ln.addView(tv, i + 1);
				// view.setExpanded(true);
				// Animation a = new ScaleAnimation(1f, 1, 0f, 1);
				// a.setInterpolator(new BounceInterpolator());
				// a.setDuration(500);
				// tv.startAnimation(a);
				// } else {
				// ln.removeViewAt(i + 1);
				// view.setExpanded(false);
				// }
			}
		});
		// view.setOnLongClickListener(new View.OnLongClickListener() {
		// @Override
		// public boolean onLongClick(View v) {
		// registerForContextMenu(v);
		// openContextMenu(v);
		// unregisterForContextMenu(v);
		// return true;
		// }
		// });
		return view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPrepareOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		buildOptionMenu(menu);

		return true;
	}

	@Override
	public void buildOptionMenu(Menu menu) {
		menu.clear();
		menu.add(0, ApplicationConstants.MENU_ID_ASK_NEW_GAME, 0, R.string.game_menu_ask_new_game);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == ApplicationConstants.MENU_ID_ASK_NEW_GAME) {
			final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			emailIntent.setType("plain/text");
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "amphiprions@gmail.com" });

			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.app_name));

			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, getString(R.string.game_menu_ask_new_game_body));
			startActivity(emailIntent);
			// context.startActivity(Intent.createChooser(intent,
			// "Send mail..."));

		}
		return true;
	}
}